package com.devunion.salon.web.action;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.devunion.salon.persistence.dao.TransactionDao;
import com.devunion.salon.persistence.dao.GiftCardDao;
import com.devunion.salon.persistence.dao.LocationDao;
import com.devunion.salon.persistence.GiftCard;
import com.devunion.salon.web.form.GiftcardForm;
import com.devunion.salon.util.TransactionCodeGenerator;
import com.devunion.salon.util.DateFormatter;

/**
 * @author Timoshenko Alexander 14.09.2008 : 14:04:24
 */
public class GiftcardAction extends CoreAction {
    private GiftCardDao giftCardDao;
    private LocationDao locationDao;

    public ActionForward save(ActionMapping actionMapping, ActionForm actionForm,
                              HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        GiftcardForm form = (GiftcardForm) actionForm;

        GiftCard giftCard = new GiftCard();
        giftCard.setAmount(Float.valueOf(form.getAmount()));
        giftCard.setCode(TransactionCodeGenerator.next());
        giftCard.setCreated(DateFormatter.getSQLDate(form.getHiddenDate()));
        giftCard.setLocation(locationDao.getLocation(getUsername()));
        giftCardDao.save(giftCard);

        form.setCode(giftCard.getCode());
        return actionMapping.getInputForward();
    }

    public void setGiftCardDao(GiftCardDao giftCardDao) {
        this.giftCardDao = giftCardDao;
    }

    public void setLocationDao(LocationDao locationDao) {
        this.locationDao = locationDao;
    }
}
